//
//  DataDidplay.m
//  HuiPinZhiJia
//
//  Created by MS on 16/9/2.
//  Copyright © 2016年 MS. All rights reserved.
//

#import "DataDidplay.h"
#import "CategoryViewController.h"
#import "HomeCell.h"
#import "DetailViewController.h"
@interface DataDidplay ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@end

@implementation DataDidplay

- (void)viewWillAppear:(BOOL)animated
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createNav];
    [self paiHang];
    [self createTableView];
   
}
#pragma mark 导航栏

- (void)createTableView
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 145, screenWidth, screenHeight - 145) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"HomeCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:_tableView];
}

#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell)
    {
        cell = [[HomeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *dvc = [[DetailViewController alloc]init];
    [self.navigationController pushViewController:dvc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (screenHeight-150)/3;

}

- (void)createNav
{
    UIView *nav = [[UIView alloc]initWithFrame:CGRectMake(0, 0,screenWidth, 80)];
    nav.backgroundColor = [UIColor colorWithRed:0.41 green:0.79 blue:1.00 alpha:1.00];

    NSMutableArray *arr = [[NSMutableArray alloc]init];
    NSArray *navigationImage = [NSArray arrayWithObjects:@"homePage",@"classify",@"search",@"shoppingCart",@"userCenter", nil];
    NSArray *name = [NSArray arrayWithObjects:@"首页",@"分类",@"搜索",@"购物车",@"用户中心",nil];
    for (int i = 0; i < 5; i++)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(10 + (screenWidth/5)*i,20,screenWidth/5 - 40 , 40);
        [btn setBackgroundImage:[UIImage imageNamed:navigationImage[i]] forState:UIControlStateNormal];
        btn.tag = i + 1;
        [btn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(5 + (screenWidth/5)*i, 55, screenWidth/5 - 20, 30)];
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:15];
        label.text = name[i];
        label.textAlignment = UITextAlignmentCenter;

        [nav addSubview:btn];
        [nav addSubview:label];
        [self.view addSubview:nav];
}
}

#pragma mark 排行
- (void)paiHang
{
    NSArray *btnName = [NSArray arrayWithObjects:@"默认",@"销量",@"人气",@"价格",@"筛选",nil];
    for (int i = 0 ; i < 5; i++)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 10000+i;
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        btn.frame = CGRectMake(screenWidth/5*i - 1, 80, screenWidth/5 + 1, 60);
        [btn setTitle:btnName[i] forState:UIControlStateNormal];
        btn.layer.borderWidth = 1;
        btn.layer.borderColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1.00].CGColor;
        [self.view addSubview:btn];
    }
    
}

- (void)btn:(UIButton *)sender
{
    UITabBarController* tab = [UIApplication sharedApplication].keyWindow.rootViewController;
    [self.navigationController popViewControllerAnimated:NO];
    tab.selectedIndex = sender.tag - 1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
